$(function () {
    layui.config({
        base: './../../../static/lib/dist/'
    });
    layui.use([ 'laydate', 'form', 'notice', 'table', 'selectPlus'],function () {
        var form = layui.form;
        var notice = layui.notice;
        var table = layui.table;
        var laydate = layui.laydate;
        var selectPlus = layui.selectPlus;

        var agencylist = null

        getChildAgencys().then(value => {
            if(value.code == 200){
                //渲染多选
                agencylist = selectPlus.render({
                    el: '#agency',
                    data: value.data,
                    type: "radio",
                    valueName: "agencyCode",
                    label: ["agencyName"],
                    values: layui.data('user').info.agencyCode
                });
            }else{
                notice.error('查询用户机构信息失败')
            }
        })

        function inittable() {
            let initjson = {
                elem: '#grouplist',
                url: 'http://127.0.0.1:8098/amc/sys/base/group/page',
                params: {
                    agencyCode: layui.data('user').info.agencyCode,
                    groupName: $('#groupName').val()
                },
                headers: {'authorization': layui.data('user').token},
                id: 'group_tab',
                cols: [[
                    {type:'checkbox', fixed: 'left'}
                    ,{field:'groupId', title: 'ID', sort: true}
                    ,{field:'groupName', edit: 'text',title: '部门名称'}
                    ,{field:'agencyCode', title: '所在机构', templet: function (res) {
                            return res.agencyInfo.agencyName
                        }}
                    ,{field:'parGroup', title: '上级部门', templet: function (res) {
                            if(res.parGroup == null){
                                return ''
                            }else{
                                return res.parGroupInfo.groupName
                            }
                        }}
                    ,{field:'createTime', title: '创建时间'}
                    ,{field:'createUser', title: '创建人', templet:function (res) {
                            if(res.createUser !=null){
                                return res.createUserInfo.name
                            }else{
                                return ''
                            }
                        }}
                    ,{field:'lastUpdateTime', title: '最近更新时间'}
                    ,{field:'lastUpdateUser', title: '最近更新人', templet:function (res) {
                            if(res.lastUpdateUser !=null){
                                return res.lastUpdateUserInfo.name
                            }else{
                                return ''
                            }
                        }}
                    ,{fixed: 'right', title:'操作', toolbar: '#row_bar', width:150}
                ]]
            }
            tableinit(table, initjson)
        }
        inittable()

        form.on('submit(search)', function (data) {
            var obj = agencylist.getChecked();
            table.reload('group_tab', {
                where: {
                    agencyCode: obj.values[0],
                    groupName: $('#groupName').val()
                } //设定异步数据接口的额外参数
            });
        })

        function del_groups(groups){
            deleteGroups(groups).then(value => {
                if(value.code == 200){
                    layer.msg('删除成功', {icon: 1});
                    tableReload()
                }else{
                    layer.msg('删除失败，'+value.msg)
                }
            }).catch(reason => {
                layer.msg('服务器未连结')
            })
        }

        $('#delAll').click(function () {
            let checkStatus = table.checkStatus('group_tab');
            let checkdata = checkStatus.data
            if(checkdata.length <= 0){
                notice.warning('请选择要删除的部门')
                return false
            }
            let groups = []
            checkdata.forEach(cd => {
                groups.push(cd.groupId)
            })
            layer.confirm('确认要删除吗？',function(index){
                del_groups(groups)
            });
        })

        function tableReload(){
            var obj = agencylist.getChecked();
            table.reload('group_tab', {
                where: {
                    agencyCode: obj.values[0],
                    groupName: $('#groupName').val()
                } //设定异步数据接口的额外参数
            });
        }

        //监听行工具事件
        table.on('tool(groupfilter)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确认要删除吗', function(index){
                    let groups = []
                    groups.push(data.groupId)
                    del_groups(groups)
                });
            } else if(obj.event === 'assign'){
                new Promise((resolve, reject) => {
                    layui.data('group', {
                        key: 'assign',
                        value: data
                    })
                    resolve('111')
                }).then(value => {
                    xadmin.open('分配职务','./group-assign.html',600,300)
                })

            }
        });

        //监听单元格编辑
        table.on('edit(groupfilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            data[field] = value
            updateGroup(data).then(value => {
                if(value.code == 200){
                    layer.msg('修改成功', {icon: 1});
                    tableReload()
                }else{
                    layer.msg('修改失败，'+value.msg)
                }
            }).catch(reason => {
                layer.msg('服务器未连结')
            })
        });
    })
})